//最⻓公共⼦序列（medium）: https://leetcode.cn/problems/longest-common-subsequence/
class Solution {
public:
    int longestCommonSubsequence(string s1, string s2) {
        int m = s1.size(), n = s2.size();
        s1 = " " + s1, s2 = " " + s2;                      // 处理下标映射关系
        vector<vector<int>> dp(m + 1, vector<int>(n + 1)); // 建表 + 初始化
        for (int i = 1; i <= m; i++)                       // 从上往下每⼀⾏
            for (int j = 1; j <= n; j++)                   // 每⼀⾏从左往右
                if (s1[i] == s2[j])
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                else
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        // 返回结果
        return dp[m][n];
    }
};

//不相交的线（medium）: https://leetcode.cn/problems/uncrossed-lines/
class Solution {
public:
    int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) {
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 4. 返回值
        int m = nums1.size(), n = nums2.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                if (nums1[i - 1] == nums2[j - 1])
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                else
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        return dp[m][n];
    }
};